const mongoose = require('mongoose');

/**
 * 搜索历史记录模型
 */
const searchHistorySchema = mongoose.Schema(
  {
    userId: {
      type: mongoose.SchemaTypes.ObjectId,
      ref: 'User',
      required: true,
      index: true
    },
    keyword: {
      type: String,
      required: true,
      trim: true
    },
    type: {
      type: String,
      enum: ['food', 'recipe'],
      default: 'food'
    },
    createdAt: {
      type: Date,
      default: Date.now,
      index: true
    }
  },
  {
    timestamps: true,
    versionKey: false
  }
);

// 创建复合索引以加快用户特定搜索历史查询
searchHistorySchema.index({ userId: 1, createdAt: -1 });

/**
 * @typedef SearchHistory
 */
const SearchHistory = mongoose.model('SearchHistory', searchHistorySchema);

module.exports = SearchHistory; 